package nl.weeaboo.sh.editor.area;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.model.TileModel;
import nl.weeaboo.sh.model.TileSetIndex;
import nl.weeaboo.sh.model.TileSetModel;

public class EditorTileSet {

	private TileSetModel model;
	private BufferedImage image;
	
	private EditorTileSet(TileSetModel model, BufferedImage image) {
		this.model = model;
		this.image = image;
	}
	
	//Functions
	public static EditorTileSet load(TileSetModel model) throws IOException {
		return new EditorTileSet(model, ImageIO.read(new File(
				String.format("res/img/tiles/%s.png", model.getId()))));
	}
	
	public void drawTileImage(Graphics2D g, TileSetIndex index, int x, int y, int w, int h, ImageObserver ob) {
		int ts = Level.tileSize;
		int sx = index.getX()*ts;
		int sy = index.getY()*ts;
		g.drawImage(image, x, y, x+w, y+h, sx, sy, sx+ts, sy+ts, ob);
	}
	
	//Getters
	public String getId() {
		return model.getId();
	}
	public TileModel getTileModel(int x, int y) {
		return model.getTileModel(x, y);
	}
	public int getHeight() {
		return model.getHeight();
	}
	public int getWidth() {
		return model.getWidth();
	}
	public TileSetModel getModel() { return model; }
	
	//Setters
	public TileModel putTile(TileModel model) {
		return this.model.putTile(model);
	}
	
}
